package com.xckj.cycling.fragment

import android.os.Bundle
import android.view.View
import android.view.ViewGroup.MarginLayoutParams
import androidx.core.widget.doAfterTextChanged
import com.junca.android.base.CViewBinding
import com.xckj.cycling.framework.MvvmUtils.lazyViewModel
import com.xckj.cycling.framework.lazyGlobalViewModel
import com.xckj.cycling.databinding.FragmentMapsBinding
import com.xckj.cycling.utils.DensityUtil
import com.xckj.cycling.utils.StatusBarHelper
import com.xckj.cycling.view.RouteMapView
import com.xckj.cycling.viewmodel.MapViewModel
import com.xckj.cycling.viewmodel.SearchHistoryViewModel

class MapFragment : BaseMapFragment() {

    private val searchViewModel: SearchHistoryViewModel by lazyViewModel()
    private val mapViewModel: MapViewModel by lazyGlobalViewModel()

    @CViewBinding
    lateinit var viewBinding: FragmentMapsBinding
    override val mapView: RouteMapView
        get() = viewBinding.maps

    override fun initView(root: View, savedInstanceState: Bundle?) {
        super.initView(root, savedInstanceState)
        viewBinding.maps.setInfoView(viewBinding.infoView)
        viewBinding.search.root.layoutParams =
            (viewBinding.search.root.layoutParams as MarginLayoutParams).apply {
                topMargin = DensityUtil.getStatusBarHeight(requireContext())
            }
        viewBinding.search.et.doAfterTextChanged {
            if (it.isNullOrEmpty()) {
                viewBinding.search.clearText.visibility = View.GONE
            } else {
                viewBinding.search.clearText.visibility = View.VISIBLE
            }
        }
        viewBinding.search.clearText.setOnClickListener {
            viewBinding.search.et.setText("")
        }
        viewBinding.search.et.setOnFocusChangeListener { v, hasFocus ->
            if (hasFocus) {
                viewBinding.search.moreArea.visibility = View.VISIBLE
            } else {
                viewBinding.search.moreArea.visibility = View.GONE
            }
        }
        viewBinding.search.clearHistory.setOnClickListener {
            searchViewModel.clearHistory()
        }
    }

    override fun resumeAndNoHidden() {
        super.resumeAndNoHidden()
        StatusBarHelper.showStatusBar(requireActivity(), true)
    }
}